In this section we cover and explain To create a height and width of the Image in details for freshers and experienced
previous | Home | Next |
Example
import java.net.*; import java.io.*; import java.awt.*; import java.awt.image.*; public class ImageSize extends Frame { TextField url; TextArea output; Button getURL; public static void main(String[] args) { ImageSize s = new ImageSize(); s.resize(300, 200); s.show(); } public ImageSize() { setLayout(new BorderLayout()); url = new TextField(40); add("North", url); output = new TextArea(80, 40); add("Center", output); add("South", new Button("Get URL")); } public boolean action(Event e, Object o) { if (e.target instanceof Button) { fixThePage(url.getText()); return true; } return false; } public void fixThePage(String thePage) { String thisLine; URL root; if (thePage != null) { //Open the URL for reading try { if (thePage.indexOf(":") != -1) { root = new URL(thePage); } else { root = new URL("http://" + thePage); } sizeAPage(root); } catch (MalformedURLException e) { System.err.println(thePage + " is not a parseable URL"); System.err.println(e); } } // end if } // end main public void sizeAPage(URL u) { char thisChar; String theTag; try { DataInputStream theHTML = new DataInputStream(u.openStream()); try { while (true) { thisChar = (char) theHTML.readByte(); if (thisChar == '<') { theTag = readTag(theHTML); if (theTag.startsWith("<IMG")) { theTag = sizeImage(u, theTag); } theOutput.appendText(theTag); } else { theOutput.appendText(String.valueOf(thisChar)); } } // end while } // end try catch (EOFException e) { // Done with the Stream } } // end try catch (IOException e) { System.err.println(e); } } // end SaveAPage public String readTag(DataInputStream is) { StringBuffer theTag = new StringBuffer("<"); char theChar = '<'; try { while (theChar != '>') { theChar = (char) is.readByte(); theTag.append(theChar); } // end while } // end try catch (EOFException e) { // Done with the Stream } catch (Exception e) { System.err.println(e); } return theTag.toString(); } public String sizeImage(URL u, String tag) { String s1 = tag.toUpperCase(); boolean hasHeightTag = s1.indexOf("HEIGHT") != -1; boolean hasWidthTag = s1.indexOf("WIDTH") != -1; if (hasHeightTag && hasWidthTag) { return tag; } else { String newTag; Image thePicture; int p1, p2, p3, p4; p1 = s1.indexOf("SRC"); p2 = s1.indexOf ("=", p1); p3 = s1.indexOf("\"", p2); p4 = s1.indexOf("\"", p3+1); String theURL = tag.substring(p3+1, p4); URL thePictureURL; try { if (theURL.indexOf(":") == -1) { // it's not an absolute URL thePictureURL = new URL(u, theURL); } // end if else { thePictureURL = new URL(theURL); } Toolkit t = Toolkit.getDefaultToolkit(); thePicture = t.getImage(thePictureURL); thePicture.getHeight(this); int last = tag.indexOf(">"); newTag = tag.substring(0,last); if (!hasWidthTag) { while (thePicture.getWidth(this) == -1) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } } newTag = newTag + " width=" + thePicture.getWidth(this); } if (!hasHeightTag) { while (thePicture.getHeight(this) == -1) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } } newTag = newTag + " height=" + thePicture.getHeight(this); } newTag = newTag + ">"; } catch (MalformedURLException e) { newTag = tag; } return newTag; } } // For this applet you don't need to // download an entire Image, just enough to get the height and width public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.HEIGHT) == ImageObserver.HEIGHT && (infoflags & ImageObserver.WIDTH) == ImageObserver.WIDTH) { return false; } else { return true; } } }
previous | Home | Next |